package com.anysoft.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lty on 2017/12/3.
 */

public abstract class BaseBaseActivity<V, T extends BasePresenter<V>> extends BaseActivity {

    protected T mPresenter;
    private Unbinder unbinder;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }
        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        setContentView(provideContentViewId());
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    
    /**
     * 用于创建Presenter和判断是否使用MVP模式(由子类实现)
     * @return
     */
    protected abstract T createPresenter();
    
    /**
     * 当前界面的布局文件id(由子类实现)
     * @return
     */
    protected abstract int provideContentViewId();
    /**
     * 初始化view
     */
    public void initView() {
    }
    /**
     * 初始化数据
     */
    public void initData() {
    }
    /**
     * 初始化监听器
     */
    public void initListener() {
    }

    
    @Override
    protected void onDestroy() {
        unbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
    }
}
